package com.xiaohe.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.xiaohe.gulimall.product.entity.AttrEntity;
import com.xiaohe.gulimall.product.service.AttrAttrgroupRelationService;
import com.xiaohe.gulimall.product.service.AttrService;
import com.xiaohe.gulimall.product.service.CategoryService;
import com.xiaohe.gulimall.product.vo.AttrGroupRelationVo;
import com.xiaohe.gulimall.product.vo.AttrGroupWithAttrsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xiaohe.gulimall.product.entity.AttrGroupEntity;
import com.xiaohe.gulimall.product.service.AttrGroupService;
import com.xiaohe.common.utils.PageUtils;
import com.xiaohe.common.utils.R;



/**
 * 属性分组
 *
 * @author hzb
 * @email 986125495@qq.com
 * @date 2021-12-16 21:37:23
 */
@RestController
@RequestMapping("product/attrgroup")
@Slf4j
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    ///product/attrgroup/{catelogId}/withattr

    /**
     * @param catelogId 所属分类id
     * @return
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        //查询当前分类下的所有属性分组
        //查询每一个分组的所有属性
        List<AttrGroupWithAttrsVo> list = attrGroupService.getAttrGroupWithAttrsByCateLogId(catelogId);
        return R.ok().put("data",list);
    }

    ///product/attrgroup/attr/relation
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> relationVo){
        relationService.saveBatchs(relationVo);
        return R.ok();
    }

    ///product/attrgroup/attr/relation/delete
    //参数[{"attrId":1,"attrGroupId":2}]
    @PostMapping("/attr/relation/delete")
    public R removeRelation(@RequestBody AttrGroupRelationVo[] vos){
        attrService.removeRelation(vos);
        return R.ok();
    }


    ///product/attrgroup/{attrgroupId}/attr/relation
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> list = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",list);
    }

    ///product/attrgroup/{attrgroupId}/noattr/relation
    //获取没有关联的分组信息
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") String attrgroupId,
                            @RequestParam Map<String, Object> params){
        PageUtils page = attrService.getNoRelation(params,attrgroupId);
        log.info("没有关联的分组信息返回数据为:<<{}>>",page);
        return R.ok().put("data",page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId){
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
		//将完整路径放进去
        Long catelogId = attrGroup.getCatelogId();
        Long[] Path = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(Path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
